# 登录，组装 令牌token = "Bearer " + resp.json().get("data")
import requests

url_login = "http://ihrm-test.itheima.net/api/sys/login"
headers_login = {"Content-Type": "application/json"}
body_login = {
    "mobile": "13800000002",
    "password": "123456"
}
resp_login = requests.post(url=url_login, headers=headers_login, json=body_login)
print(resp_login.json())

token = "Bearer " + resp_login.json().get("data")
# print(token)

# 新增员工, 获取员工id， resp.json().get("data").get("id")
url_add = "http://ihrm-test.itheima.net/api/sys/user"
headers_add = {
    "Content-Type": "application/json",
    "Authorization": token
}
body_add = {
    "username": "YG001",
    "mobile": "18210223333",
    "workNumber": "123"
}
resp_add = requests.post(url=url_add, headers=headers_add, json=body_add)
print(resp_add.json())

user_id = resp_add.json().get("data").get("id")

# 查询员工
url_query = f"http://ihrm-test.itheima.net/api/sys/user/{user_id}"
headers_query = headers_add
resp_query = requests.get(url=url_query, headers=headers_query)
print(resp_query.json())

# 修改员工
url_modify = f"http://ihrm-test.itheima.net/api/sys/user/{user_id}"
headers_modify = headers_add
body_modify = {
    "username": "YG002",
    "password": "123456",
    "departmentId": "10086"
}

resp_modify = requests.put(url=url_modify, headers=headers_modify, json=body_modify)
print(resp_modify.json())
# 删除员工
url_del = f"http://ihrm-test.itheima.net/api/sys/user/{user_id}"
headers_del = headers_add
resp_del = requests.delete(url=url_del, headers=headers_del)
print(resp_del.json())
